#!/usr/bin/env python
# -*- coding: utf-8 -*-
from urllib.parse import parse_qsl

import requests

__author__ = 'Txc001'

def make_session():
    s = requests.session()
    s.verify = False
    s.trust_env = False
    s.headers = {
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36',
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
    }

    return s

def print_headers_raw_to_dict(headers_raw_l):
    print("{\n    '" + ",\n    ".join(map(lambda s: "'" +
        "': '".join(s.strip().split(':')) + "'", headers_raw_l))[1:-1] + "'\n}")

def print_headers_raw_to_dict_space(headers_raw_l):
    print("{\n    '" + ",\n    ".join(map(lambda s: "'" + "': '".join(s.strip().split('\t') if len(s.strip().split('\t'))>1 else [s.strip(), '']) + "'", headers_raw_l))[1:-1] + "'\n}")

def print_dict_from_copy_headers(headers_raw):
    headers_raw = headers_raw.strip()
    headers_raw_l = headers_raw.splitlines()

    if 'HTTP/1.1' in headers_raw_l[0]:
        headers_raw_l.pop(0)
    if headers_raw_l[0].startswith('Host'):
        headers_raw_l.pop(0)
    if headers_raw_l[-1].startswith('Cookie'):
        headers_raw_l.pop(-1)

    if ':' in headers_raw_l[-1] and ':' in headers_raw_l[0]:
        print_headers_raw_to_dict(headers_raw_l)
    else:
        print_headers_raw_to_dict_space(headers_raw_l)

def print_url_params(url_params):
    s = str(parse_qsl(url_params.strip(), 1))
    print("OrderedDict(\n    " + "),\n    ".join(map(lambda s: s.strip(), s.split("),")))[1:-1] + ",\n)")

def print_url_params_new(url_params):
    l = parse_qsl(url_params.strip(), 1)
    print("{\n    " + "',\n    ".join(map(lambda s: "'"+s[0]+"': '"+s[1], l)) + "',\n}")

if __name__ == '__main__':
    text = '''
Connection	keep-alive
Cache-Control	max-age=0
Upgrade-Insecure-Requests	1
User-Agent	Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36
Accept	text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer	https://www.lagou.com/jobs/list_html5?city=%E5%85%A8%E5%9B%BD&cl=false&fromSearch=true&labelWords=&suginput=
Accept-Encoding	gzip, deflate, br
Accept-Language	zh-CN,zh;q=0.9
Cookie	user_trace_token=20180720164250-eb82fc9b-f8cc-4467-a734-e14728842c2c; _ga=GA1.2.786864854.1532076171; LGUID=20180720164252-e3faa058-8bf8-11e8-9fcf-525400f775ce; index_location_city=%E5%85%A8%E5%9B%BD; LG_LOGIN_USER_ID=e094d940657c9dad82f3d2e92f5bdf6b54fd0bbdf82c806668c151bc53c5799d; JSESSIONID=ABAAABAAADEAAFI9699497106E3B44A3A2A07E30C690B65; _gid=GA1.2.1300180617.1534315688; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1532076171,1534316193; SEARCH_ID=b57e5f2b346a44bbaad3d3ce5ecfb7bc; TG-TRACK-CODE=search_code; _gat=1; LGSID=20180815160116-62f1401d-a061-11e8-a3a0-5254005c3644; PRE_UTM=; PRE_HOST=; PRE_SITE=https%3A%2F%2Fwww.lagou.com%2Fjobs%2Flist_html5%3Fcity%3D%25E5%2585%25A8%25E5%259B%25BD%26cl%3Dfalse%26fromSearch%3Dtrue%26labelWords%3D%26suginput%3D; PRE_LAND=https%3A%2F%2Fwww.lagou.com%2Fjobs%2F5002596.html; X_HTTP_TOKEN=226c9fb4d99de31539268a84bd58f419; Hm_lpvt_4233e74dff0ae5bd0a3d81c6ccf756e6=1534320079; LGRID=20180815160134-6d9faffb-a061-11e8-a3a0-5254005c3644
    '''

    print_dict_from_copy_headers(text)

    # url_params = 'random=1533606146003&tourFlag=dc&_json_att&REPEAT_SUBMIT_TOKEN=332a20557110c0ed7c4a5aa742d72e02'
    # print_url_params_new(url_params)